﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using ResponseObject;
using BartonExtension;

namespace Backend
{
    public class WebServiceHelper
    {
        public static void LoadBrands(Action<List<Brand>,Exception> action)
        {
            PostParams param = new PostParams();
            param.addParam("page_index", 0);
            param.addParam("page_capacity", 1000);
            param.addParam("lan_tag", "cn");
            HttpPost post = new HttpPost("{0}/apis/ZK_GeneralService.asmx/brands".FormatWith(AddressPool.ServerAddr), param.jsonSerialize());
            post.post((json, err) =>
            {
                if (err != null)
                {
                    action(null, err);
                    return;
                }
                ZKResponse response = json.FromJson<Dictionary<string, object>>()["d"].ToString().FromJson<ZKResponse>();
                if (response.isProcessSuccess)
                {
                    List<Brand> brands = response.userinfo.ToString().FromJson<List<Brand>>();
                    action(brands, null);
                }
            });
        }

        public static void LoadCatalogs(Action<List<Catalog>, Exception> action)
        {
            PostParams param = new PostParams();
            param.addParam("lan_tag", "all");
            HttpPost post = new HttpPost("{0}/apis/ZK_GeneralService.asmx/catalogs".FormatWith(AddressPool.ServerAddr), param.jsonSerialize());
            post.post((json, err) =>
            {
                if (err != null)
                {
                    action(null, err);
                    return;
                }
                ZKResponse response = json.FromJson<Dictionary<string, object>>()["d"].ToString().FromJson<ZKResponse>();
                if (response.isProcessSuccess)
                {
                    List<Catalog> catalogs = response.userinfo.ToString().FromJson<List<Catalog>>();
                    action(catalogs, null);
                }  
                else
                {
                    action(null,new Exception(response.errMsg));
                }
            });
        }

        public static void LoadMalls(Action<List<Mall>,Exception> action)
        {
            HttpPost post = new HttpPost("{0}/apis/ZK_GeneralService.asmx/malls".FormatWith(AddressPool.ServerAddr),"");
            post.get((json, err) =>
                {
                    if (err != null)
                    {
                        action(null, err);
                        return;
                    }
                    ZKResponse response = json.FromJson<Dictionary<string, object>>()["d"].ToString().FromJson<ZKResponse>();
                    if (response.isProcessSuccess)
                    {
                        List<Mall> malls = response.userinfo.ToString().FromJson<List<Mall>>();
                        action(malls, null);
                    }
                    else
                    {
                        action(null,new Exception(response.errMsg));
                    }
                });
        }

        public static void LoadStores(Action<List<NewStore>, Exception> action)
        {
            PostParams param = new PostParams();
            HttpPost post = new HttpPost("{0}/apis/ZK_GeneralService.asmx/stores".FormatWith(AddressPool.ServerAddr), param.jsonSerialize());
            post.post((s, e) =>
            {
                if (e != null)
                {
                    action(null, e);
                    return;
                }
                ZKResponse response = s.FromJson<Dictionary<string, object>>()["d"].ToString().FromJson<ZKResponse>();
                if (response.isProcessSuccess)
                {
                    List<NewStore> stores = response.userinfo.ToString().FromJson<List<NewStore>>();
                    action(stores, null);
                }
                else
                {
                    action(null, new Exception(response.errMsg));
                }
            });
        }

        public static void LoadActs(Action<List<Activity>,Exception> action)
        {
            PostParams param = new PostParams();
            HttpPost post = new HttpPost("{0}/apis/ZK_GeneralService.asmx/activities".FormatWith(AddressPool.ServerAddr), param.jsonSerialize());
            post.post((s, e) =>
                {
                    if (e != null)
                    {
                        action(null, e);
                        return;
                    }
                    ZKResponse response = s.FromJson<Dictionary<string, object>>()["d"].ToString().FromJson<ZKResponse>();
                    if (response.isProcessSuccess)
                    {
                        List<Activity> acts = response.userinfo.ToString().FromJson<List<Activity>>();
                        action(acts, null);
                    }
                    else
                    {
                        action(null, new Exception(response.errMsg));
                    }
                });
        }
    }
}
